//
//  ViewController.m
//  03-图片浏览器
//
//  Created by wangneihan on 15/12/9.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"
#import "ImgsData.h"

#define BTN_TAG_LEFT 1
#define BTN_TAG_RIGHT 2

@interface ViewController ()

@property (nonatomic,strong) IBOutlet UILabel *indexLabel;

@property (nonatomic,strong) IBOutlet UILabel *descLabel;

@property (nonatomic,strong) IBOutlet UIImageView *imgsView;

@property (nonatomic,strong) IBOutlet UIButton *leftBtn;

@property (nonatomic,strong) IBOutlet UIButton *rightBtn;

@property (nonatomic) int index;

@property (nonatomic,strong) NSMutableDictionary *imgsDic;

@property (nonatomic,strong) NSArray *array;


-(IBAction)btnClick:(UIButton *) btn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 需要用到的控件  uilabel uiimageview  uibutton
    // 1. 创建2个 UILabel
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat width = size.width;
    CGFloat height = size.height;
    
    //图集进度 label
    self.indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(width/2-25, 20, 50, 20)];
    [self.indexLabel setBackgroundColor:[UIColor grayColor]];
    [self.indexLabel setTextAlignment:NSTextAlignmentCenter];
    self.indexLabel.text = @"1/5";
    
    //UIIamgeView
    self.imgsView = [[UIImageView alloc] initWithFrame:CGRectMake(width*0.2, 30, width*0.6, 340)];
    [self.imgsView setImage:[UIImage imageNamed:@"bingli.png"]];
    [self.imgsView setContentMode:UIViewContentModeScaleAspectFit];

    //左右翻得按钮
    //1. left 50,50
    self.leftBtn = [[UIButton alloc] initWithFrame:CGRectMake((width*0.2 - 55), 185, 50, 50)];
    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal.png" ] forState:UIControlStateNormal];
    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted.png" ] forState:UIControlStateHighlighted];
    [self.leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.leftBtn setTag:BTN_TAG_LEFT];
    
    self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(width*0.8 + 5, 185, 50, 50)];
    [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal.png" ] forState:UIControlStateNormal];
    [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted.png" ] forState:UIControlStateHighlighted];
    [self.rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightBtn setTag:BTN_TAG_RIGHT];
    
    self.descLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 380, width, 20)];
    [self.descLabel setBackgroundColor:[UIColor grayColor]];
    [self.descLabel setText:@"你好，这是UILabel的自动换行测试内容，主要实现多行数据的自动换行，自适应不同行数的数据"];
    self.descLabel.numberOfLines = 0;
    //[summylabel setTextAlignment:NSTextAlignmentCenter];
    //[summylabel setLineBreakMode:uiline];
   // summylabel.text = @"0/0";
    //文字居中显示
    //summylabel.textAlignment = n;
    //自动折行设置
//    summylabel.lineBreakMode = NSLineBreakByCharWrapping;
//    CGSize size1 = [summylabel sizeThatFits:CGSizeMake(summylabel.frame.size.width, MAXFLOAT)];
//    summylabel.frame =CGRectMake(10, 100, 300, size1.height);
    
    [self.view addSubview:self.indexLabel];
    [self.view addSubview:self.imgsView];
    [self.view addSubview:self.leftBtn];
    [self.view addSubview:self.rightBtn];
    [self.view addSubview:self.descLabel];

    self.index = 0;
    [self showImg:self.index];
}

-(IBAction)btnClick:(UIButton *) btn {
    if(btn.tag==BTN_TAG_LEFT)
    {
        self.index--;
    }
    else if(btn.tag==BTN_TAG_RIGHT)
    {
       self.index++;
    }
    [self showImg:self.index];
}

-(void)showImg:(int) tempIndex{
    NSLog(@"序号 %d,index %d",tempIndex,self.index);
//    ImgsData *tempImg = [self GetImg:tempIndex];
//    self.imgsView.image = [UIImage imageNamed:[tempImg fileName]];
//    self.indexLabel.text = [NSString stringWithFormat:@"%d/5",self.index + 1];
//    self.descLabel.text = tempImg.descStr;
    NSDictionary *tempImg = self.array[self.index];
    
    self.imgsView.image = [UIImage imageNamed:tempImg[@"icon"]];
    self.indexLabel.text = [NSString stringWithFormat:@"%d/%lu",self.index + 1,(unsigned long)self.array.count];
    self.descLabel.text = tempImg[@"desc"];
    
    self.leftBtn.enabled = (self.index != 0);
    self.rightBtn.enabled = (self.index != 4);
    
}

-(ImgsData *)GetImg:(int) index{
    if(self.imgsDic)
    {
        ImgsData *result = [self.imgsDic objectForKey:[NSString stringWithFormat:@"%d",index]];
        return result;
    }
    else{
        [self LoadImgs];
        return [self GetImg:index];
    }
    
}

-(void)LoadImgs{
    
    ImgsData *img0 = [[ImgsData alloc] initWithIndex:0 andFileName:@"biaoqingdi" andDesc:@"神表情！！！"];
    ImgsData *img1 = [[ImgsData alloc] initWithIndex:1 andFileName:@"bingli" andDesc:@"木有肛门？？？！！！"];
    ImgsData *img2 = [[ImgsData alloc] initWithIndex:2 andFileName:@"chiniupa" andDesc:@"吃个牛排至于么？"];
    ImgsData *img3 = [[ImgsData alloc] initWithIndex:3 andFileName:@"danteng" andDesc:@"奇葩"];
    ImgsData *img4 = [[ImgsData alloc] initWithIndex:4 andFileName:@"wangba" andDesc:@"无语"];
    self.imgsDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                    img0,[NSString stringWithFormat:@"%d",img0.index] ,
                    img1,[NSString stringWithFormat:@"%d",img1.index] ,
                    img2,[NSString stringWithFormat:@"%d",img2.index] ,
                    img3,[NSString stringWithFormat:@"%d",img3.index] ,
                    img4,[NSString stringWithFormat:@"%d",img4.index] ,
                    nil];
    
}

//懒加载
-(NSArray *) array{
    if(_array == nil)
    {
        _array=@[@{@"1":@"2",@"2":@"3"}];
        //应用所在的文件夹
        NSString *path = [[NSBundle mainBundle] bundlePath];
        //获取plist的全路径
        NSString *path2 = [[NSBundle mainBundle] pathForResource:@"ImgPaths" ofType:@"plist"];
        //只要是方法名称后面是File 那么要路径就是全路径（绝对路径）
        _array = [NSArray arrayWithContentsOfFile:path2];
    }
    return _array;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
